<?php

namespace Sjj\Eloquent\Concerns;

trait HidesAttributes
{
	/**
	 * The attributes that should be hidden for serialization.
	 *
	 * @var array
	 */
	protected $hidden = [];

	/**
	 * The attributes that should be visible in serialization.
	 *
	 * @var array
	 */
	protected $visible = [];

	/**
	 * Get the hidden attributes for the model.
	 *
	 * @return array
	 */
	public function getHidden()
	{
		return $this->hidden;
	}

	/**
	 * Set the hidden attributes for the model.
	 *
	 * @param array $hidden
	 * @return $this
	 */
	public function setHidden(array $hidden)
	{
		$this->hidden = $hidden;

		return $this;
	}

	/**
	 * Add hidden attributes for the model.
	 *
	 * @param array|string|null $attributes
	 * @return void
	 */
	public function addHidden($attributes = null)
	{
		$this->hidden = array_merge(
			$this->hidden, is_array($attributes) ? $attributes : func_get_args()
		);
	}

	/**
	 * Get the visible attributes for the model.
	 *
	 * @return array
	 */
	public function getVisible()
	{
		return $this->visible;
	}

	/**
	 * Set the visible attributes for the model.
	 *
	 * @param array $visible
	 * @return $this
	 */
	public function setVisible(array $visible)
	{
		$this->visible = $visible;

		return $this;
	}

	/**
	 * Add visible attributes for the model.
	 *
	 * @param array|string|null $attributes
	 * @return void
	 */
	public function addVisible($attributes = null)
	{
		$this->visible = array_merge(
			$this->visible, is_array($attributes) ? $attributes : func_get_args()
		);
	}

	/**
	 * Make the given, typically hidden, attributes visible.
	 *
	 * @param array|string $attributes
	 * @return $this
	 */
	public function makeVisible($attributes)
	{
		$this->hidden = array_diff($this->hidden, (array)$attributes);

		if (!empty($this->visible)) {
			$this->addVisible($attributes);
		}

		return $this;
	}

	/**
	 * Make the given, typically visible, attributes hidden.
	 *
	 * @param array|string $attributes
	 * @return $this
	 */
	public function makeHidden($attributes)
	{
		$attributes = (array)$attributes;

		$this->visible = array_diff($this->visible, $attributes);

		$this->hidden = array_unique(array_merge($this->hidden, $attributes));

		return $this;
	}

	/**
	 * @param string|array $visible
	 * @return array
	 */
	protected function parseVisible($visible): array
	{
		if (is_array($visible)) {
			return $visible;
		} elseif (is_string($visible)) {
			$fields = explode(',', $visible);
			$visible = array_map('trim', $fields);
		} else {
			$visible = [];
		}
		return $visible;
	}
}
